package shili;

import java.lang.reflect.Method;

public class ShiLi9 {
    public static void main(String[] args) throws Exception {
        Class clz = Class.forName("reflect.entity.Person");

        //根据方法名和参数列表获取static final int getAge()方法，
        //没有参数可以不写或用null表示
        Method getAge = clz.getDeclaredMethod("getAge",null);

        getAge.setAccessible(true);

        Object returnAge = getAge.invoke(null,null);
        System.out.println("年龄是: " + returnAge);

        Object person = clz.newInstance();


        Method silentMethod = clz.getDeclaredMethod("silentMethod",null);


        silentMethod.setAccessible(true);

        silentMethod.invoke(person,null);

        Method setName = clz.getDeclaredMethod("setName",String.class);

        setName.invoke(person,"New Person");

        Object returnName = clz.getDeclaredMethod("getName").invoke(person);
        System.out.println("刚才设定的name是: " + returnName);
    }
}
